package com.wzh.test.leetcode;

import com.sun.scenario.effect.impl.prism.PrImage;

public class MyLinkedList {

    public Node head;
    public Node tail;

    public void addNode(Node node){
        if(head == null){
            head = node;
            tail = node;
        }else{
            tail.next = node;
            tail = node;
        }
    }

    public void printNode(){
        Node tmpNode = head;
        while (tmpNode != null){
            System.out.print(tmpNode.data + "===>");
            tmpNode = tmpNode.next;
        }
    }

    public void reverseWhile(){
        if(this.head != null){
            this.tail = this.head;
            Node preNode = null;
            Node node = this.head;
            while (node != null){
                Node tmpNode = node.next;
                node.next = preNode;
                preNode = node;
                node = tmpNode;
            }
            this.head = preNode;
        }
    }
    public void reverse(){
        Node tmpNode = head;
        if(tmpNode != null){
            reverse(tmpNode,tmpNode.next);
            this.tail = tmpNode;
            this.tail.next = null;
        }
    }

    private void reverse(Node preNode,Node node){
        if(node != this.tail){
            reverse(node,node.next);
        }else{
            this.head = node;
        }
        node.next = preNode;
    }

    public static class Node{
        public String data;
        public Node next;

        public Node(String data){
            this.data = data;
        }
    }


}
